package com.xywh.wanandroid.data.repository

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.xywh.wanandroid.data.model.bean.ArticleItem
import com.xywh.wanandroid.data.model.bean.BannerBean
import com.xywh.wanandroid.data.model.bean.CollectArticlesData
import com.xywh.wanandroid.data.model.bean.KnowledgeTreeItem
import com.xywh.wanandroid.data.model.bean.NavigationBeanItem
import com.xywh.wanandroid.data.model.bean.ProjectTreeItem
import com.xywh.wanandroid.data.model.bean.RankItem
import com.xywh.wanandroid.data.model.bean.UserScoreBeanItem
import com.xywh.wanandroid.data.model.bean.WXChapterItem
import com.xywh.wanandroid.data.repository.datasource.CollectDataSource
import com.xywh.wanandroid.data.repository.datasource.HomeArticleDataSource
import com.xywh.wanandroid.data.repository.datasource.HomeBannersDataSource
import com.xywh.wanandroid.data.repository.datasource.KnowledgeArticleDataSource
import com.xywh.wanandroid.data.repository.datasource.KnowledgeTreeDataSource
import com.xywh.wanandroid.data.repository.datasource.NavigationListDataSource
import com.xywh.wanandroid.data.repository.datasource.ProjectArticleDataSource
import com.xywh.wanandroid.data.repository.datasource.ProjectTreeDataSource
import com.xywh.wanandroid.data.repository.datasource.RankDataSource
import com.xywh.wanandroid.data.repository.datasource.SearchDataSource
import com.xywh.wanandroid.data.repository.datasource.SquareArticleDataSource
import com.xywh.wanandroid.data.repository.datasource.TopArticlesDataSource
import com.xywh.wanandroid.data.repository.datasource.UserScoreDataSource
import com.xywh.wanandroid.data.repository.datasource.WXChaptersDataSource
import com.xywh.wanandroid.data.repository.datasource.WechatArticleDataSource
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@ViewModelScoped
class WanAndroidRepository @Inject constructor() {

    private val pageConfigCommon = PagingConfig(
        pageSize = 10,
        initialLoadSize = 20, // 第一次加载数量，如果不设置的话是 pageSize * 3
        prefetchDistance = 2, // 距离下一页请求的距离
    )
    private val pageConfigMax = PagingConfig(pageSize = Int.MAX_VALUE)

    fun fetchHomeBannerList(): Flow<PagingData<BannerBean>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigMax,
            pagingSourceFactory = {
                HomeBannersDataSource()
            }
        ).flow
    }

    fun fetchHomeTopArticlesList(): Flow<PagingData<ArticleItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigMax,
            pagingSourceFactory = {
                TopArticlesDataSource()
            }
        ).flow
    }

    fun fetchHomeArticleList(): Flow<PagingData<ArticleItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigCommon,
            pagingSourceFactory = {
                HomeArticleDataSource()
            }
        ).flow
    }

    fun fetchSquareArticleList(): Flow<PagingData<ArticleItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = PagingConfig(
                pageSize = 10,
                initialLoadSize = 20, // 第一次加载数量，如果不设置的话是 pageSize * 3
                prefetchDistance = 2, // 距离下一页请求的距离
            ),
            pagingSourceFactory = {
                SquareArticleDataSource()
            }
        ).flow
    }

    fun fetchWechatArticleList(id: Int): Flow<PagingData<ArticleItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigCommon,
            pagingSourceFactory = {
                WechatArticleDataSource(id)
            }
        ).flow
    }

    fun fetchWXChaptersList(): Flow<PagingData<WXChapterItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigMax,
            pagingSourceFactory = {
                WXChaptersDataSource()
            }
        ).flow
    }

    fun fetchProjectTreeList(): Flow<PagingData<ProjectTreeItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigMax,
            pagingSourceFactory = {
                ProjectTreeDataSource()
            }
        ).flow
    }

    fun fetchProjectArticleList(cid: Int): Flow<PagingData<ArticleItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigCommon,
            pagingSourceFactory = {
                ProjectArticleDataSource(cid)
            }
        ).flow
    }

    fun fetchKnowledgeTreeList(): Flow<PagingData<KnowledgeTreeItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigMax,
            pagingSourceFactory = {
                KnowledgeTreeDataSource()
            }
        ).flow
    }

    fun fetchKnowledgeArticleList(cid: Int): Flow<PagingData<ArticleItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigCommon,
            pagingSourceFactory = {
                KnowledgeArticleDataSource(cid)
            }
        ).flow
    }

    fun fetchNavigationList(): Flow<PagingData<NavigationBeanItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigMax,
            pagingSourceFactory = {
                NavigationListDataSource()
            }
        ).flow
    }


    fun fetchRankDataList(): Flow<PagingData<RankItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigCommon,
            pagingSourceFactory = {
                RankDataSource()
            }
        ).flow
    }

    fun fetchCollectDataList(): Flow<PagingData<CollectArticlesData>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigCommon,
            pagingSourceFactory = {
                CollectDataSource()
            }
        ).flow
    }

    fun fetchSearchDataList(key: String): Flow<PagingData<ArticleItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigCommon,
            pagingSourceFactory = {
                SearchDataSource(key)
            }
        ).flow
    }

    fun fetchUserScoreDataList(): Flow<PagingData<UserScoreBeanItem>> {
        // 通过Pager.flow返回流对象
        return Pager(
            config = pageConfigMax,
            pagingSourceFactory = {
                UserScoreDataSource()
            }
        ).flow
    }

}